package c2.cross.business.guildwar.common;

import c2.advanced.business.base.KvData;
import cate.common.table.d.GDGuildWar;
import cate.common.util.XT;
import cate.game.pvp.guildwar.data.GWGuildProfile;
import cate.game.pvp.guildwar.data.base.GWMatch;
import cate.game.pvp.guildwar.data.base.GuildWarBase;
import cate.game.pvp.guildwar.data.base.GuildWarTimeTable;
import cate.game.pvp.guildwar.data.rank.GWChampion;
import cate.game.pvp.guildwar.data.rank.GWGuildRank;
import easy.java.dev.note.NoteField;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class GuildWarStatus extends KvData {

    @NoteField("上次跨天时间")
    public long lastDaySpanTime;

    @NoteField("当前赛季基础数据")
    public GuildWarBase base;

    @NoteField("历届冠军")
    public Map<Integer, GWChampion> champions;

    @NoteField("单日公会战时间表")
    public GuildWarTimeTable timeTable;

    @NoteField("匹配信息")
    public GWMatch match;

    @Override
    public void init(String theKey) {
        super.init(theKey);
        if (lastDaySpanTime == 0L) {
            lastDaySpanTime = System.currentTimeMillis();
        }
        if (base == null) {
            base = new GuildWarBase();
        }
        base.initialize();
        if (champions == null) {
            champions = new HashMap<>();
        }
        if (timeTable == null) {
            timeTable = new GuildWarTimeTable();
        }
        timeTable.initialize();
        if (match == null) {
            match = new GWMatch();
        }
        match.initialize();
    }

    public void handleSign(int srvId, List<GWGuildProfile> list) {
        if (XT.isBlank(list)) {
            log.error("跨服接收到{}服的公会战报名数据但是报名列表为空", srvId);
            return;
        }
        if (timeTable.period != GDGuildWar.Period.SIGN_UP) {
            log.error("跨服接收到[{}服]的公会战报名数据但是目前不是报名阶段", srvId);
            return;
        }
        match.handleSign(srvId, list);
    }

    public void setChampion(GWGuildRank top) {
        champions.put(base.index, GWChampion.of(top.profile.srvId, top.profile.name, top.profile.guildBanner));
    }

    public void onDaySpan(){
        lastDaySpanTime = System.currentTimeMillis();
        timeTable.clear();
        match.clear();
    }
}
